package com.cmse.persistence.dao.impl;

import java.sql.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.cmse.persistence.dao.IArticalDao;
import com.cmse.persistence.domain.Artical;
import com.cmse.persistence.enmu.FetchOrderType;

/**
 * @author wulibin, exceptsky@gmail.com, 2012-3-14 上午10:17:18
 * 
 */
@SuppressWarnings("rawtypes")
public class ArticalDao extends BaseDao<Artical> implements IArticalDao{
	private static Logger          logger           = Logger.getLogger(ArticalDao.class);
	
	@Override
	public List<Artical> getByDate(Date startDate, Date endDate, int offset, int length, FetchOrderType orderType) {
		String hql = "from Artical as a where a.createdAt between ? and ?"+getOrderSql("a", orderType);
		return selectByPage(hql, new Date[]{startDate, endDate}, offset, length);
	}
	
	@Override
	public long getCountByDate(Date startDate, Date endDate) {
		String hql = "select count(a.id) from Artical as a where a.createdAt between ? and ?";
		try {
			List rs = getHibernateTemplate().find(hql, new Date[]{startDate, endDate});
			if(rs==null || rs.size()==0){
				return 0l;
			}
			long count = (Long)rs.listIterator().next();
			return count;
		} catch (Exception e) {
			logger.error(e);
			return 0l;
		}
	}

}
